import pygame

from game import SCREEN_WIDTH, SCREEN_HEIGHT

"""
按钮相关常量类
"""


class ButtonAtr:
    bg_location = './resources/images/welcome/welcome1.jpg'
    pygame.init()
    screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
    bg = pygame.image.load(bg_location)
    font_addr = pygame.font.get_default_font()
    font = pygame.font.Font(font_addr, 36)


"""
按钮类
"""


class Button(object):
    def __init__(self, text, color, x=None, y=None, **kwargs):
        """
        初始化
        :param text: 文本
        :param color: 颜色
        :param x:
        :param y:
        :param kwargs: bool
        """
        self.surface = ButtonAtr.font.render(text, True, color)

        self.WIDTH = self.surface.get_width()
        self.HEIGHT = self.surface.get_height()

        if 'centered_x' in kwargs and kwargs['centered_x']:
            self.x = SCREEN_WIDTH // 2 - self.WIDTH // 2
        else:
            self.x = x

        if 'centered_y' in kwargs and kwargs['cenntered_y']:
            self.y = SCREEN_HEIGHT // 2 - self.HEIGHT // 2
        else:
            self.y = y

    def display(self):
        """
        显示出按钮
        :return:
        """
        ButtonAtr.screen.blit(self.surface, (self.x, self.y))

    def check_click(self, position):
        """
        检测鼠标是否触碰到按钮
        :param position:
        :return:
        """
        x_match = self.x < position[0] < self.x + self.WIDTH
        y_match = self.y < position[1] < self.y + self.HEIGHT

        if x_match and y_match:
            return True
        else:
            return False
